<?php
class GiftcateAction extends MbaseAction {
	public $tbname = 'Giftcate';
	public $obj = 'Giftcate';
	public $mpk = 'giftcate_id';
	public $cate = '';
	public function index() {
		return false;
	}
	public function show() {
		$mpk_val = isset ($_GET[$this->mpk]) ? $_GET[$this->mpk] : 0;
		$mgood = M('Good');
		if (!$mpk_val)
			$this->error('无数据', 0);
		$data[$this->mpk] = $mpk_val;
		$datainfo = $this->obj->doGet(array (
			'q' => $data
		));
		if (!$datainfo) {
			$this->error($this->obj->getError(), 0);
			return false;
		} else {
			foreach ($datainfo['cate'] as $k => $vo) {
				$datainfo['cate'][$k]['cate_ids'] = explode(',', $vo['cate_ids']);
			}
			foreach ($datainfo['cate'] as $k => $vo) {
				$good_ids = explode(',', $vo['good_ids']);
				$datainfo['cate'][$k]['goods'] = array ();
				foreach ($good_ids as $gvo) {
					$tmap['good_id'] = $gvo;
					$datainfo['cate'][$k]['goods'][] = $mgood->where($tmap)->find();
				}
			}
			$this->assign('datainfo', $datainfo);
			$this->setSeo($datainfo['title'], $datainfo['keyword'], $datainfo['detail']);
			$this->getHotlist();
			$this->getSpc();
			$picnews = $this->getNewsRela($datainfo['news_keyword'],1,2);
			//dump($picnews);
			$newslist = $this->getNewsRela($datainfo['news_keyword']);
			//dump($newslist);
			$this->assign('newspic',$picnews);
			$this->assign('newslist',$newslist);
			$this->display();
		}
	}
	public function getHotlist() {
		$mgood = D('Good');
		$map = array (
			'q' => $this->map['q'],
			'o' => array (
				'oby' => 'order_count',
				'okey' => 'desc'
			),
			'p' => 1,
			'ps' => 10
		);
		$sellgoods = $mgood->doGet($map);
		$this->assign('selllist', $sellgoods['list']);
	}

	public function getSpc($cate_id = 0) {
		$mn = M('News');
		$catemap = $this->getCateMap($cate_id);
		$catemap['is_topic'] = 1;
		$catemap['thumb'] = array (
			'neq',
			''
		);
		$datalist = $mn->where($catemap)->order('clicked desc')->limit(2)->select();
		$this->assign('spclist1', $datalist);
		$catemap['thumb'] = '';
		$datalist = $mn->where($catemap)->order('clicked desc')->limit(10)->select();
		$this->assign('spclist2', $datalist);
	}

	public function getNewsRela($keywords = '', $thumb = 0, $num = 10) {
		$keywords = trim($keywords);
		if (empty ($keywords))
			return null;
		$map['tag'] = array (
			'in',
			$keywords
		);
		$mtag = M('Tag');
		$mnt = M('News_tag');
		$mn = M('News');
		$tag_ids = $mtag->where($map)->field('tag_id')->select();
		$tids = array ();
		foreach ($tag_ids as $vo) {
			$tids[] = $vo['tag_id'];
		}
		$ntmap['tag_id'] = array (
			'in',
			implode(',', $tids)
		);
		$news_ids = $mnt->where($ntmap)->group('news_id')->field('news_id')->limit($num)->select();
		$tnids = array ();
		foreach ($news_ids as $vo) {
			$tnids[] = $vo['news_id'];
		}
		$nmap['news_id'] = array (
			'in',
			implode(',', $tnids)
		);
		$nmap['thumb'] = empty($thumb)? '':array('neq','');
		$datalist = $mn->where($nmap)->select();
		return $datalist;
	}
}
?>